package org.lq.shopping.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.lq.shopping.dao.ShopShengqDao;
import org.lq.shopping.dao.impl.ShopShengqDaoImpl;
import org.lq.shopping.entity.ShopShengq;
import org.lq.shopping.service.ShopShengqService;

import com.google.gson.Gson;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class ShopShengqServiceImpl implements ShopShengqService {

	static ShopShengqDao dao = new ShopShengqDaoImpl();
	//private ShopShengq shengq;
	private static Gson gson = new Gson();
	@Override
	public boolean addShopShengq(ShopShengq ssq) {
		return dao.add(ssq)>0;
	}

	@Override
	public boolean updateShopShengq(ShopShengq ssq) {
		return dao.update(ssq)>0;
	}

	@Override
	public List<ShopShengq> findPage(int pageIndex, int pageSize) {
		Jedis jedis = setRedis();
		int min = (pageIndex-1)*pageSize;
		int max = pageIndex*pageSize;
		List<ShopShengq> pageList = new ArrayList<>();
		Set<String> list =jedis.zrangeByLex("shengqSet", "-", "+",min, max);
		//List<String> list = jedis.lrange("shengqList", min, max);
		for (String sheng : list) {
			ShopShengq shengq = gson.fromJson(sheng, ShopShengq.class);
			pageList.add(shengq);
		}
		return pageList;
	}

	@Override
	public List<ShopShengq> findPageByUser(int uid, int pageIndex, int pageSize) {
		Jedis jedis = setRedisByUid(uid);
		int min = (pageIndex-1)*pageSize;
		int max = pageIndex*pageSize;
		List<ShopShengq> getByUidlist = new ArrayList<>();
		Set<String> list = jedis.zrangeByLex("shengByUidSet", "-", "+",min, max);
		//List<String> list = jedis.lrange("shengByUidList", min, max);
		for (String sheng : list) {
			ShopShengq shengq = gson.fromJson(sheng, ShopShengq.class);
			getByUidlist.add(shengq);
		}
		return getByUidlist;
	}

	@Override
	public List<ShopShengq> getListByOrderId(int order_id) {
		return dao.getListByOrderId(order_id);
	}

	/**
	 * redis连接对象
	 * @return
	 */
	public static Jedis getRedis() {
		Jedis jedis = null;
		JedisPoolConfig config = new JedisPoolConfig();
		//控制最大连接数
		config.setMaxTotal(50);
		//设置空闲链接
		config.setMaxIdle(5);
		//默认-1,等待时间,若等待时间内没有获取连接,则抛异常
		config.setMaxWaitMillis(1000*100);
		//在获取连接之前先进性,validate操作,验证!保证到连接是可用的
		config.setTestOnBorrow(true);
		//当返回一个连接给pool的时候先进性ping()判断是否可以连接
		config.setTestOnReturn(true);
		
		JedisPool pool = new JedisPool(config,"127.0.0.1",6379,2000,"123456");
		jedis = pool.getResource();
		return jedis;
	}
	/**
	 * 将查询(查询全部)出来的列表存在redis
	 * @return
	 */
	public static Jedis setRedis() {
		Jedis jedis = getRedis();
		List<ShopShengq> list = dao.findAll();
		for (ShopShengq shopShengq : list) {
			String json = gson.toJson(shopShengq);
			jedis.zadd("shengqSet",shopShengq.getAfter_id() ,json);
		}

		return jedis;
	}
	/**
	 * 将查询(根据用户id)出来的列表存在redis
	 * @return
	 */
	public static Jedis setRedisByUid(int uid) {
		Jedis jedis = getRedis();
		List<ShopShengq> list = dao.getListByUId(uid);
		for (ShopShengq shopShengq : list) {
			String json = gson.toJson(shopShengq);
			jedis.zadd("shengByUidSet",shopShengq.getAfter_id(),json);
		}
		return jedis;
	}
}
